package com.zhuiYun.project.api.commoditycollection.service.Impl;

import com.zhuiYun.project.api.commoditycollection.entity.CommodityCollection;
import com.zhuiYun.project.api.commoditycollection.entity.dto.CommodityCollectionDTO;
import com.zhuiYun.project.api.commoditycollection.mapper.CommodityCollectionMapper;
import com.zhuiYun.project.api.commoditycollection.model.CommodityCollectionModel;
import com.zhuiYun.project.api.commoditycollection.service.CommodityCollectionService;
import com.zhuiYun.project.common.errorCode.CommonException;
import com.zhuiYun.project.common.errorCode.EmErrorCode;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.BeanUtils;
import com.zhuiYun.project.util.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CommodityCollectionServiceImpl
 * @Description TODO
 * @Author yangze
 * @Date 2023/4/5 9:21
 **/
@Service
public class CommodityCollectionServiceImpl implements CommodityCollectionService {

    @Resource
    CommodityCollectionMapper commodityCollectionMapper;

    /**
     * @param commodityCollectionDTO
     * @Author yangze
     * @Description TODO 查询用户商品收藏信息
     * @Date 2023/4/5 16:56
     * @Return CommonResult
     */

    @Override
    public CommonResult selectByUserId(CommodityCollectionDTO commodityCollectionDTO) throws CommonException {
        if (commodityCollectionDTO.getUserId() !=null) {
            List<CommodityCollectionDTO> commodityCollectionDTOS = commodityCollectionMapper.selectByUserId(commodityCollectionDTO);
            if (ObjectUtils.isNotEmpty(commodityCollectionDTOS)){
                return CommonResult.ok(commodityCollectionDTOS);
            } else {
                return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
            }
        }else {
            return CommonResult.error(EmErrorCode.OBJECT_NOT_NULL);
        }
    }

/**
* @Author yangze
* @Description TODO 用户取消收藏
* @Date 2023/4/5 16:57
* @Return CommonResult
* @param id
*/

    @Override
    public CommonResult deleteById(Integer id)throws CommonException {
        if (id==null){
            return CommonResult.error(EmErrorCode.OBJECT_NOT_NULL);
        }else{
            commodityCollectionMapper.deleteById(id);
            return CommonResult.ok();
        }
    }
/**
* @Author 杨泽
* @Description 根据用户ID收藏商品
* @Date 2023/4/5 16:57
* @Return  CommonResult
* @param commodityCollectionModel
*/

    @Override
    public CommonResult insertCollection(CommodityCollectionModel commodityCollectionModel) throws CommonException{
       //判断是否已经收藏过该商品
        CommodityCollection commodityCollection = commodityCollectionMapper.selectCollection(commodityCollectionModel.getUserId(), commodityCollectionModel.getCommodityId());
        if(commodityCollection != null) {
           return CommonResult.ok(commodityCollectionMapper.updateCollection(commodityCollectionModel.getUserId(), commodityCollectionModel.getCommodityId()));
        }else {
            CommodityCollection commodityCollection1 = new CommodityCollection();
            BeanUtils.copy(commodityCollectionModel, commodityCollection1);
            commodityCollection1.setCreateTime(new Date());
            commodityCollection1.setDelFlag(0);

            return CommonResult.ok(commodityCollectionMapper.insertSelective(commodityCollection1));
        }

    }
/**
* @Author yangze
* @Description  TODO 判断用户是否收藏过该商品
* @Date 2023/4/10 16:08
* @Return
* @param userId
* @param commodityId
*/

    @Override
    public CommonResult selectCollection(Integer userId, Integer commodityId) {
        CommodityCollection commodityCollection = commodityCollectionMapper.selectCollection(userId, commodityId);
        if(commodityCollection == null){
            return CommonResult.ok(false);
        }else{
            if(commodityCollection.getDelFlag() == 1){
                return CommonResult.ok(false);
            }
            //如果收藏记录不为空 且delFlag为0，则返回true 代表有真实收藏记录
            return CommonResult.ok(true);
        }
    }
    /**
     * @Author yangze
     * @Description 根据用户userid和commodityId商品id取消收藏
     * @Date 2023/4/10 15:19
     * @Return
     * @param commodityCollectionModel
     */
    @Override
    public CommonResult deleteByUserIdAndDiyId(CommodityCollectionModel commodityCollectionModel) {
        return CommonResult.ok(commodityCollectionMapper.deleteByUserIdAndCommodityId(commodityCollectionModel.getUserId(),commodityCollectionModel.getCommodityId()));
    }

}
